package com.alibaba.shopping_goods_service.service;

import com.alibaba.shopping_common.pojo.Specification;
import com.alibaba.shopping_common.pojo.SpecificationOption;
import com.alibaba.shopping_common.pojo.SpecificationOptions;
import com.alibaba.shopping_common.service.SpecificationService;
import com.alibaba.shopping_goods_service.mapper.SpecificationMapper;
import com.alibaba.shopping_goods_service.mapper.SpecificationOptionMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName SpecificationImpl
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/9 17:36
 */
@DubboService // 开启dubbo注册
@Transactional // 开启事务管理
public class SpecificationImpl implements SpecificationService {

    @Autowired
    private SpecificationMapper specificationMapper;

    @Autowired
    private SpecificationOptionMapper optionMapper;

    @Override
    public void add(Specification specification) {
        specificationMapper.insert(specification);
    }

    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            // 删除商品规格表中的数据
            specificationMapper.deleteById(id);
            // 删除商品规格项中的数据,ID非主键所以需要自己构造查询条件
            QueryWrapper<SpecificationOption> wrapper = new QueryWrapper<>();
            wrapper.eq("specid",id);
            optionMapper.delete(wrapper);
        }
    }

    @Override
    public void update(Specification specification) {
        specificationMapper.updateById(specification);
    }

    // 根据产品类型ID查询器所有的规格项
    @Override
    public Specification findById(Long id) {
        return specificationMapper.findById(id);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Specification> search(int page, int size) {
        return specificationMapper.selectPage(new Page<>(page, size), null);
    }

    @Override
    public List<Specification> findByProductTypeId(Long id) {
        // 根据产品类型ID查询其下所有的规格项
        return specificationMapper.findByProductTypeId(id);
    }

    // 批量新增
    @Override
    public void addOption(SpecificationOptions options) {
        Long specId = options.getSpecId();
        String[] optionName = options.getOptionName();
        // 批量新增
        for (String name : optionName) {
            SpecificationOption specificationOption = new SpecificationOption();
            // 规格项名称
            specificationOption.setOptionName(name);
            // 规格ID
            specificationOption.setSpecId(specId);
            optionMapper.insert(specificationOption);
        }
    }

    /**
     * 批量删除规格项
     * @param ids 规格id的数组
     */
    @Override
    public void deleteOption(Long[] ids) {
        optionMapper.deleteBatchIds(Arrays.asList(ids));
    }
}




